package com.example.perftool2;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * adb shell ls -al /storage/emulated/0/perf-tool/dump*
 * adb shell cat /storage/emulated/0/perf-tool/dump*
 */
public class FileWriter {

    private static final String TAG = FileWriter.class.getSimpleName();
    private String FILE_SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/perf-tool/";
    private String FILE_NAME = "dump.csv";
    private File mOutFile;
    private boolean isFileInited;

    public FileWriter(Context context) {

    }

    public void initFile() {
        if (isFileInited) return;
        isFileInited = true;
        File storage = new File(FILE_SAVE_PATH);
        if (!storage.exists()) {
            storage.mkdirs();
        }
        mOutFile = new File(FILE_SAVE_PATH, "dump_" + getTimeStamp(false) + ".csv");
        Log.i(TAG, "openFile, path:" + FILE_SAVE_PATH + ", file:" + mOutFile.getAbsolutePath());
        if (mOutFile.exists()) {
            Log.i(TAG, "删除原有文件");
            mOutFile.delete();
        }
        if (!mOutFile.exists()) {
            Log.i(TAG, "文件删除成功");
            try {
                mOutFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        save(false, constructTitle());
    }

    public static final String[] titles = new String[]  {
            "时间",
            "pid",
            "uid",
            "名称",
            "CPU(cpuinfo)",
            "CPU(top)",
            "CPU_idle",
            "GPU",
            "Mem",
            "Mem%",
            "Total Free Mem",
            "温度",
            "耗电",
            "下载",
            "上传"
    };

    public static final char COMMA = ',';

    private String constructTitle() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < titles.length; i++) {
            sb.append(titles[i]);
            sb.append(i == titles.length - 1 ? '\n' : COMMA);
        }
        return sb.toString();
    }

    public void save(boolean append, String value) {
        Log.i(TAG, "#### save, value:" + value);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(mOutFile, append);
            fileOutputStream.write(value.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getTimeStamp(boolean inCsv) {
        String pattern = inCsv ? "HH:mm:ss" : "MMdd_HH_mm_ss";
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date());
    }
}
